package com.uncp.comisiondeadmision.comprobantedepago.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


import com.uncp.comisiondeadmision.comprobantedepago.beans.CargarTotalBean;
import com.uncp.comisiondeadmision.comprobantedepago.beans.ModalidadBean;
import com.uncp.comisiondeadmision.util.MySqlDBConn;





///////////////////////corregir mandarlo como parametro el igv 
public class MySqlModalidadDAO implements ModalidadDAO {
	
	//HistorialIgvServicios IgvServicio = new HistorialIgvServicios();

	public CargarTotalBean Costo(int cod) throws SQLException {
		
		CargarTotalBean bean=null;
		
		// TODO Auto-generated method stub
		Connection conn = null;
		PreparedStatement pst = null;
		
		ResultSet rs = null;
		
		
		
		//double igv=IgvServicio.calcularIgv();
		try {
			conn = new MySqlDBConn().getConnection();
			String sql = "SELECT distinct t.costo,h.valor FROM tipomodalidad t join historial_igv h where codtipomodalidad="+cod+" order by h.fecha desc limit 1;";
			pst = conn.prepareStatement(sql);
			
			rs = pst.executeQuery();
		

			while (rs.next()) {
				double igv=(rs.getDouble("h.valor"));
				double costo=(rs.getDouble("t.costo"));
				double total=(costo*igv)+costo;
				bean = new CargarTotalBean();
				bean.setDblCosto(costo);
				bean.setDblIgv(igv);
				bean.setDblTotal(total);
				
	
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (rs != null)	 rs.close();
			if (pst != null) pst.close();
			if (conn != null)conn.close();
		}

		return bean;
	}

	@Override
	public ModalidadBean CargarModalidad(int cod) throws SQLException {
		ModalidadBean bean=null;
		
		// TODO Auto-generated method stub
		Connection conn = null;
		PreparedStatement pst = null;
		
		ResultSet rs = null;
		
		
		try {
			conn = new MySqlDBConn().getConnection();
			String sql = "SELECT CODTIPOMODALIDAD, NOMBRE, COSTO FROM tipomodalidad where codtipomodalidad="+cod;
		
			pst = conn.prepareStatement(sql);
			
			rs = pst.executeQuery();
		

			while (rs.next()) {
				bean = new ModalidadBean();
				bean.setIntCodTipoModalidad(rs.getInt("CODTIPOMODALIDAD"));
				bean.setStrDescripcion(rs.getString("NOMBRE"));
				bean.setDblCosto(rs.getDouble("costo"));
				
	
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (rs != null)	 rs.close();
			if (pst != null) pst.close();
			if (conn != null)conn.close();
		}

		return bean;
	}
	

}


